#### 常见的异常类
## SyntaxError: invalid syntax
# 语法错误
# if 1 > 0::
#     print("ok")

## IndentationError: unexpected indent
# 缩进错误
#   print("ok")

## IndexError: list index out of range
# 下标超出范围
# a = [1, 2, 3]
# print(a[3])

## KeyError: 'c'
# 关键字错误
# dict = {'a': 1, 'b': 2}
# dict['c']

## AttributeError: module 'math' has no attribute 'xxx'
# 属性错误
# import math
# math.xxx()

## ValueError: invalid literal for int() with base 10: 'abc'
# 参数传递类型错误
# int("abc")

## FileNotFoundError: [Errno 2] No such file or directory: 'lsdjf.txt'
# 没人发现这个文件
# fp = open("lsdjf.txt")

## AssertionError
# 断言错误
# assert 0 > 1


# ----------------------------------------------------------------------
#### 异常捕获
# try:
#     print("start try")
#     a = 1/0
#     print("end try...")
# except ZeroDivisionError as ze:
#     # 发生除数异常时，要做的事情
#     print("zero error: ", ze)
# print("ending...")
# print()
#
# try:
#     print("start try")
#     b = [1, 2, 3]
#     print(b[3])
#     print("end try...")
# except IndexError as ie:
#     print("IndexError: ", ie)
# print("ending...")
# print()
#
# try:
#     print("start try")
#     b = [1, 2, 3]
#     print(b[3])
#     print("end try...")
# except IndexError as ie:
#     print("IndexError: ", ie)
# print("ending...")

## else结构 --> 没有任何异常发生时侯执行else
# try:
#     a = 1/0
#     print("else...")
# except Exception:
#     print("exception...")
# else:
#     print("this is else test")

## finally结构 --> 不管有没有异常都会执行
# 没有捕获到异常：
# try:
#     print("start...")
#     a = 1/0
# except IndexError:
#     print("this is index error")
# finally:
#     print("this is finally")

# 捕获到异常：
# try:
#     print("start...")
#     a = 1 / 0
# except ZeroDivisionError:
#     print("this is zero error")
# finally:
#     print("this is finally")


# ----------------------------------------------------------------------------
#### 异常嵌套
# try:
#     pass
#     try:
#         pass
#     except:
#         pass
# except:
#     pass

# def aaa():
#     try:
#         a = "hello world"
#         try:
#             int(a)
#         except:
#             print("value error")
#             # finally的优先级比return高
#             return "i am value"
#         finally:
#             print("i am finally1")
#         print(name)
#     except:
#         print("name error")
#     finally:
#         print("i am finally2")
#
# print(aaa())
# finally的优先级比return高
# 不管怎样我们捕获到一个异常后下面的都不会再进行下去
# value error
# i am finally1
# i am finally2
# i am value


# ------------------------------------------------------------------------
#### 手动触发异常 --> 同一时间写一个
# raise 异常类（"说明信息"）
raise Exception("LJLJFLJFLAJLD")